package iot.core.util;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.PrettyPrinter;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;

import java.util.List;
@Component

public   class MyJSON {
    /**
     * 实例化ObjectMapper对象
     */


    static MappingJackson2HttpMessageConverter  mappingJackson2HttpMessageConverter;
    public static  ObjectMapper objectMapper;
    /**
     * 私有化构造器
     */
       static {

    }
    @Autowired
    void setMappingJackson2HttpMessageConverter(MappingJackson2HttpMessageConverter arg){
        MyJSON.mappingJackson2HttpMessageConverter=arg;

        init();
    }
    void init(){
        objectMapper=mappingJackson2HttpMessageConverter.getObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);

      //  objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,
//                ObjectMapper.DefaultTyping.NON_FINAL);
        objectMapper.registerModule(simpleModule);

    }
    /**
     * 对象转json字符串
     *
     * @param obj obj
     * @return
     */
    public static   String toJSONString(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 对象转json字符串
     *
     * @param obj    obj
     * @return
     */
    public  static String toJSONPrettyString(Object obj) {
        try {

            SerializationConfig config = objectMapper.getSerializationConfig();
            PrettyPrinter prettyPrinter = config.getDefaultPrettyPrinter();
            DefaultPrettyPrinter def = (DefaultPrettyPrinter) prettyPrinter;
            DefaultPrettyPrinter.Indenter indented = new DefaultIndenter("   ", DefaultIndenter.SYS_LF);
            def.indentArraysWith(indented);
            def.indentObjectsWith(indented);
            ObjectWriter objectWriter = objectMapper.writer(def);
            return objectWriter.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * json字符串转换bean
     *
     * @param clazz   clazz
     * @param jsonStr jsonStr
     * @return
     */
    public static   <T> T parseObject(Class<T> clazz, String jsonStr) {
        try {

            return objectMapper.readValue(jsonStr, clazz);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * json字符串转对象集合
     *
     * @param jsonStr jsonStr
     * @param clazz   clazz
     * @return
     */
    public static   <T> List<T> parseArray(String jsonStr, Class<T> clazz) {

        JavaType javaType = objectMapper.getTypeFactory().constructCollectionType(List.class, clazz);
        try {
            return objectMapper.readValue(jsonStr, javaType);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

}
